function newVector = shipMarkov(currentVector)

%parameters definition
velocity = 10;

%transition probs
pL2L = 0.9;
pL2M = 0.1;
pM2L = 0.05;
pM2M = 0.9;
pM2R = 0.05;
pR2M = 0.1;
pR2R = 0.9;

%emission probs
pLinL = 0.9;
pMinL = 0.05;
pRinL = 0.05;
pLinM = 0.05;
pMinM = 0.9;
pRinM = 0.05;
pLinR = 0.05;
pMinR = 0.05;
pRinR = 0.9;

persistent actualState; %0->Left, 1->Middle, 2->Right
curVel = norm(currentVector); %current velocity

if(curVel == 0) %means that this is the first turn
    rn = rand(1); %random direction
    if rn<1/3
        actualState=0;
    elseif rn<2/3
        actualState=1;
    else
        actualState=2;
    end
    newDir = rand(1,2)*2-1;                         %completely random direction
    newVec = newDir/norm(newDir);
else
    normVector = currentVector/curVel;              %normalized vector
    %current direction = inv tangent(y/x) in degrees
    if(normVector(1)==0)
        if(normVector(2)>0)
            curDir = 90;
        else
            curDir = 270;
        end
    elseif(normVector(1)>0)
        curDir = atand(normVector(2)/normVector(1));
    else
        curDir = atand(normVector(2)/normVector(1))+180;
    end
    
    %find the new state
    rn = rand(1);
    if actualState==0
        if rn<pL2L
            actualState=0;
        else
            actualState=1;
        end
    elseif actualState==1
        if rn<pM2L
            actualState=0;
        elseif rn<pM2L+pM2M
            actualState=1;
        else
            actualState=2;
        end
    elseif actualState==2
        if rn<pR2M
            actualState=1;
        else
            actualState=2;
        end
    end
    
    %calcualte the cahnge of the direction
    rn = rand(1);
    if actualState==0
        if rn<pLinL
            changeDir = rand(1)*30-45;
        elseif rn<pLinL+pMinL
            changeDir = rand(1)*30-15;
        else
            changeDir = rand(1)*30+15;
        end
    elseif actualState==1
        if rn<pLinM
            changeDir = rand(1)*30-45;
        elseif rn<pLinM+pMinM
            changeDir = rand(1)*30-15;
        else
            changeDir = rand(1)*30+15;
        end
    elseif actualState==2
        if rn<pLinR
            changeDir = rand(1)*30-45;
        elseif rn<pLinR+pMinR
            changeDir = rand(1)*30-15;
        else
            changeDir = rand(1)*30+15;
        end
    end
    newDir = curDir + changeDir;                 %current direction  + random number in interval [-45,45]
    if(newDir>=360)
        newDir = newDir-360;
    elseif(newDir<=-360)
        newDir = newDir+360;
    end
    newVec = [cosd(newDir), sind(newDir)];             %calculate the new unit vector
end

newVector = velocity*newVec;

end